﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-16 11:09:49
 * 功能：炸弹效果
**************************************************************************************/

using DG.Tweening;
using Game.Framework;
using Spine;
using Spine.Unity;
using System;
using UnityEngine;
using static Game.BaseData.Config.Server;

namespace Game.UI.GamePanel
{
    public class BombAnim : MonoBehaviour
    {
        private SkeletonGraphic m_Anim;
        private RectTransform m_Rect;
        private Item m_Tar;

        private void Find()
        {
            m_Anim = GetComponent<SkeletonGraphic>();
            m_Rect = transform as RectTransform;
        }

        public void Init(Item tar)
        {
            if (m_Anim == null) Find();
            m_Tar = tar;
            //transform.localScale = Vector3.one * .8f;
            transform.localScale = Vector3.zero;
            var tarScreen = Camera.main.WorldToScreenPoint(tar.Icon.transform.position);
            RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent as RectTransform, tarScreen, Camera.main, out var pos);
            LogManager.Log($"pos:{pos}", "炸弹目标位置");
            m_Anim.AnimationState.SetAnimation(0, "static", true);

            // 创建动画序列
            Sequence throwSequence = DOTween.Sequence();
            float throwDuration = 0.5f;

            // 1. 抛物线移动动画
            throwSequence.Join(
                DOTween.To(
                    () => m_Rect.anchoredPosition,
                    x => m_Rect.anchoredPosition = x,
                    pos,
                    throwDuration
                ).SetEase(Ease.OutQuad)
            );

            // 2. 缩放动画（近大远小效果）
            // 先放大再缩小到原始大小
            throwSequence.Join(
                m_Rect.DOScale(Vector3.one * 1.5f, throwDuration / 2)
                    .SetEase(Ease.OutQuad)
                    .OnComplete(() =>
                    {
                        m_Rect.DOScale(Vector3.one * 1.2f, throwDuration / 2)
                            .SetEase(Ease.InQuad);
                    })
            );

            // 添加旋转动画
            throwSequence.Join(m_Rect.DORotate(new Vector3(0, 0, 360), throwDuration, DG.Tweening.RotateMode.FastBeyond360));

            // 3. 到达目标位置后播放爆炸效果
            throwSequence.OnComplete(() =>
            {
                // 播放爆炸动画
                m_Anim.AnimationState.ClearListenerNotifications();
                var data = m_Anim.AnimationState.SetAnimation(0, "dynamic", false);
                //m_Rect.localScale = Vector3.one;
                DOVirtual.DelayedCall(0.4f, () => m_Tar.UpdateUIStatus());
                data.Complete += data => PoolManager.Instance.Delete(GamePanelNotifications.BOMB_ITEM_POOL, gameObject);
            });

            throwSequence.Play();
        }
    }
}